package com.example.servicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
	private Button startserviceButton,stopserviceButton,bindserviceButton,unbindserviceButton;
	private MyService.DownloadBinder downloadBinder;
	private Button startintentserviceButton;
	private ServiceConnection connection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			downloadBinder= (MyService.DownloadBinder)service;
			downloadBinder.startDownload();
			downloadBinder.getProgress();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startserviceButton=(Button)findViewById(R.id.startservice);
		stopserviceButton=(Button)findViewById(R.id.stopservice);
		bindserviceButton=(Button)findViewById(R.id.bindservice);
		unbindserviceButton=(Button)findViewById(R.id.unbindservice);
		startintentserviceButton=(Button)findViewById(R.id.startintentservice);
		startserviceButton.setOnClickListener(this);
		stopserviceButton.setOnClickListener(this);
		bindserviceButton.setOnClickListener(this);
		unbindserviceButton.setOnClickListener(this);
		startintentserviceButton.setOnClickListener(this);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.startservice:
			Intent startinteIntent = new Intent(this,MyService.class);
			startService(startinteIntent);
			break;
		case R.id.stopservice:
			Intent stopIntent = new Intent(this,MyService.class);
			stopService(stopIntent);
			break;
		case R.id.bindservice:
			Intent bindIntent = new Intent(this,MyService.class);
			bindService(bindIntent,connection,BIND_AUTO_CREATE);
			break;
		case R.id.unbindservice:
			unbindService(connection);
			break;
		case R.id.startintentservice:
			Log.d("MainActivity", "Thread id is "+Thread.currentThread().getId());
			Intent intentserrviceIntent = new Intent(this,MyIntentService.class);
			startService(intentserrviceIntent);
			break;
		default:
			break;
		}
	}
}








